package com.muyu.auth;

import com.muyu.danmo.domain.DanmoInformation;
import com.muyu.danmo.domain.DanmoInformationBar;
import com.muyu.danmo.domain.DanmoInformationContent;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class ExcelReader {
    public static void main(String[] args) {
        String excelFilePath = "C:\\Users\\Administrator\\Desktop\\新建 XLSX 工作表.xlsx"; // Excel文件路径
        readExcelAndPrintInfo(excelFilePath);
    }


    public static void readExcelAndPrintInfo(String filePath) {

        try (InputStream inputStream = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(inputStream)) {
            boolean isFirstRow = true;
            Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
            String previousName  = null;
            DanmoInformation danmoInformation = new DanmoInformation();
            DanmoInformationBar danmoInformationBar = new DanmoInformationBar();
            List<DanmoInformationContent> danmoInformationContents = new ArrayList<>();
            for (Row row : sheet) {
                // 获取单元格数据
                Cell chineseName = row.getCell(0);
                Cell fontSize = row.getCell(1);
                /**
                 * 入会城市
                 */
                Cell membership  = row.getCell(2);
                Cell city = row.getCell(3);
                Cell district = row.getCell(4);
                Cell avatar = row.getCell(5);
                Cell  foreignName= row.getCell(6);
                Cell nationality = row.getCell(7);
                Cell national = row.getCell(8);
                Cell birthplace = row.getCell(9);
                Cell dateBirth = row.getCell(10);
                Cell graduateSchool = row.getCell(11);
                Cell vocational = row.getCell(12);
                Cell representativeWorks = row.getCell(13);
                Cell mainAchievements = row.getCell(14);
                Cell positions = row.getCell(15);


                Cell parent  = row.getCell(16);
                Cell rich = row.getCell(17);
                Cell atlas = row.getCell(18);
                Cell sort = row.getCell(19);
                Cell association = row.getCell(20);
                Cell profile = row.getCell(21);
                Cell year = row.getCell(22);
                Cell whichSession = row.getCell(23);
                Cell duties = row.getCell(24);

                danmoInformationBar.setChineseName(String.valueOf(chineseName));
                danmoInformationBar.setFontSize(String.valueOf(fontSize));
                danmoInformationBar.setNationality(String.valueOf(nationality));
                danmoInformationBar.setNational(String.valueOf(national));
                danmoInformationBar.setBirthplace(String.valueOf(birthplace));
                danmoInformationBar.setDateBirth(String.valueOf(dateBirth));
                danmoInformationBar.setForeignName(String.valueOf(foreignName));
                danmoInformationBar.setGraduateSchool(String.valueOf(graduateSchool));
                danmoInformationBar.setVocational(String.valueOf(vocational));
                danmoInformationBar.setRepresentativeWorks(String.valueOf(representativeWorks));
                danmoInformationBar.setMainAchievements(String.valueOf(mainAchievements));
                danmoInformationBar.setPositions(String.valueOf(positions));
                danmoInformationBar.setProfile(String.valueOf(profile));

                danmoInformation.setYear(String.valueOf(year));
                danmoInformation.setAssociation(Long.valueOf(1));
                danmoInformation.setAddressEntry(String.valueOf(membership));
                 danmoInformation.setAddressCity(String.valueOf(city));
                 danmoInformation.setAddressCounty(String.valueOf(district));

                DanmoInformationContent danmoInformationContent = new DanmoInformationContent();
                danmoInformationContent.setParentHeading(String.valueOf(parent));
                danmoInformationContent.setRichText(String.valueOf(rich));
                danmoInformationContent.setAtlas(String.valueOf(atlas));
                danmoInformationContent.setSort(Long.valueOf(String.valueOf(sort)));
                danmoInformationContents.add(
                        danmoInformationContent
                );
                System.out.println("----------------------------------------");
                if (previousName != null && String.valueOf(chineseName).equals(previousName)) {
                    // 当前行的中文名与上一行相同，提取信息
                    String primaryTitle = row.getCell(16).getStringCellValue();
                    String richText = row.getCell(17).getStringCellValue();
                    String atlas1 = row.getCell(18).getStringCellValue();
                    String sort1 = row.getCell(19).getStringCellValue();
                    DanmoInformationContent danmoInformationContent1 = new DanmoInformationContent();
                    danmoInformationContent1.setParentHeading(String.valueOf(parent));
                    danmoInformationContent1.setRichText(String.valueOf(rich));
                    danmoInformationContent1.setAtlas(String.valueOf(atlas));
                    danmoInformationContent1.setSort(Long.valueOf(String.valueOf(sort)));
                    danmoInformationContents.add(
                            danmoInformationContent
                    );
                    System.out.println("一级标题："+ primaryTitle);
                    System.out.println("富文本："+ richText);
                    System.out.println("图集（多张图片用逗号隔开）："+ atlas1);
                    System.out.println("排序："+ sort1);
//                        personInfoList.add(personInfo);

                } else {
                    // 更新上一行的中文名
                    previousName = String.valueOf(chineseName);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

//                    System.out.println("中文名: " + chineseName.getStringCellValue());
//                    System.out.println("字号: " + fontSize.getStringCellValue());
//                    System.out.println("入会城市: " + Membership.getStringCellValue());
//                    System.out.println("市: " + city.getStringCellValue());
//                    System.out.println("区: " + district.getStringCellValue());
//                    System.out.println("头像: " + avatar.getStringCellValue());
//                    System.out.println("洋文名字: " + foreignName.getStringCellValue());
//                    System.out.println("国籍: " + nationality.getStringCellValue());
//                    System.out.println("民族: " + national.getStringCellValue());
//                    System.out.println("出生地: " + birthplace.getStringCellValue());
//                    System.out.println("代表作品: " + dateBirth.getStringCellValue());
//                    System.out.println("主要成就: " + graduateSchool.getStringCellValue());
//                    System.out.println("职位（百度）: " + vocational.getStringCellValue());
//                    System.out.println("职业: " + representativeWorks.getStringCellValue());
//                    System.out.println("出生日期: " + mainAchievements.getStringCellValue());
//                    System.out.println("毕业院校: " + positions.getStringCellValue());
//                    System.out.println("简介: " + profile.getStringCellValue());
//                    System.out.println("富文本: " + rich.getStringCellValue());
//                    System.out.println("一级标题: " + parent.getStringCellValue());
//                    if (parent == null){
//                        System.out.println("图集（多张图片用逗号隔开）: "+ "暂无");
//                    }else {
//                        System.out.println("图集（多张图片用逗号隔开）: " + atlas.getStringCellValue());
//                    }
//                    System.out.println("排序: " + sort.getStringCellValue());
//                    System.out.println("书法协会/社会团体" + association.getStringCellValue());
//                    System.out.println("简介： " + profile.getStringCellValue());
//                    System.out.println("年份： " + year.getStringCellValue());
//                    System.out.println("第几届（理事会）： " + whichSession.getStringCellValue());
//                    System.out.println("职务（理事会）： " + duties.getStringCellValue());
